﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;

namespace _056.XML操作
{
	internal class Program
	{
		static void Main(string[] args)
		{

			List<Skill> skills = new List<Skill>();
			// 解析XML文档
			XmlDocument xmlDoc = new XmlDocument();
			// 加载 路径的形式
			//xmlDoc.Load("Skills.xml");
			// 字符串的形式
			xmlDoc.LoadXml(File.ReadAllText("Skills.xml"));

			// FirstChild 是文档的声明
			//XmlNode root = xmlDoc.FirstChild;
			XmlNode root = xmlDoc.ChildNodes[1];
			XmlNodeList skillList = root.ChildNodes;
			//遍历
			foreach (XmlNode skill in skillList)
			{
				Skill skillObj = new Skill();
				// 遍历的方式
				//XmlNodeList list = skill.ChildNodes;
				//foreach (XmlNode node in list)
				//{
				//	//Console.WriteLine(node.Name+ ":"+ node.InnerText);
				//	if (node.Name == "id")
				//	{
				//		skillObj.ID = Int32.Parse(node.InnerText);
				//	}
				//	else if (node.Name == "name")
				//	{
				//		skillObj.Name = node.InnerText;
				//		skillObj.Lang = node.Attributes[0].Value;
				//	}
				//	else
				//	{
				//		skillObj.Damage = Int32.Parse(node.InnerText);
				//	}
				//}

				// 索引器的方式
				XmlElement idEle = skill["id"];
				skillObj.ID = Int32.Parse(idEle.InnerText);
				XmlElement nameEle = skill["name"];
				
				skillObj.Name = nameEle.InnerText;
				XmlAttributeCollection attrCol = nameEle.Attributes;
				XmlAttribute  attri= attrCol["lang"];
				skillObj.Lang = attri.Value;

				XmlElement damageEle = skill["damage"];
				skillObj.Damage = Int32.Parse(damageEle.InnerText);


				skills.Add(skillObj);
			}

			


			foreach (Skill skill in skills)
			{
				Console.WriteLine(skill.ID + "," + skill.Name + "," + skill.Lang + "," + skill.Damage);
			}
		}
	}
}
